package com.fulihui.systemcore.service;

import com.fulihui.systemcore.api.ApiTemplateService;
import com.fulihui.systemcore.dal.dataobj.ApiTemplate;
import com.fulihui.systemcore.dal.mapper.ApiTemplateMapper;
import com.fulihui.systemcore.dto.ApiTemplateDTO;
import com.fulihui.systemcore.repository.ApiTemplateRepository;
import com.fulihui.systemcore.request.apitemplate.ApiTemplateCreateRequest;
import com.fulihui.systemcore.request.apitemplate.ApiTemplateUpdateRequest;
import com.google.common.collect.Lists;
import org.near.servicesupport.error.CommonServiceErrorEnum;
import org.near.servicesupport.request.TPageRequest;
import org.near.servicesupport.request.TRequest;
import org.near.servicesupport.result.BaseResult;
import org.near.servicesupport.result.ResultBuilder;
import org.near.servicesupport.result.TPageResult;
import org.near.servicesupport.result.TSingleResult;
import org.near.servicesupport.util.ServiceAssert;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.inject.Inject;
import java.util.List;

/**
 * Created by  lz on 2017-5-31.
 */
@Service("apiTemplateService")
public class ApiTemplateServiceImpl implements ApiTemplateService {
    @Autowired
    private ApiTemplateRepository apiTemplateRepository;

    @Autowired
    private ApiTemplateMapper     apiTemplateMapper;

    /**
     * 创建http-api服务来源
     *
     * @param request the request
     * @return the t single result
     */
    @Override
    public TSingleResult<Long> save(ApiTemplateCreateRequest request) {
        ServiceAssert.notNull(request, CommonServiceErrorEnum.REQUEST_PARAMETER_ERROR);

        ApiTemplateDTO apiTemplateDTO = apiTemplateRepository.queryByApiCode(request.getApiCode());
        if (apiTemplateDTO != null) {
            return ResultBuilder.failTSingle(101, "apiCode 已经存在");
        }
        ApiTemplateDTO dto = new ApiTemplateDTO();
        BeanUtils.copyProperties(request, dto);
        Long aLong = apiTemplateRepository.create(dto);
        return ResultBuilder.succTSingle(aLong);
    }

    /**
     * Update t single result.
     *
     * @param request the request
     * @return the t single result
     */
    @Override
    public TSingleResult<Long> update(ApiTemplateUpdateRequest request) {
        ServiceAssert.notNull(request, CommonServiceErrorEnum.REQUEST_PARAMETER_ERROR);
        ApiTemplateDTO dto = apiTemplateRepository.queryByPk(request.getId());
        if (dto == null) {
            return ResultBuilder.failTSingle(101, "配置信息不存在");
        }

        ApiTemplate r = new ApiTemplate();
        BeanUtils.copyProperties(request, r);
        apiTemplateMapper.updateByPrimaryKeySelective(r);
        return ResultBuilder.succTSingle(dto.getId());
    }

    /**
     * Query page t page result.
     *
     * @param request the request
     * @return the t page result
     */
    @Override
    public TPageResult<ApiTemplateDTO> queryPage(TPageRequest request) {

        ServiceAssert.notNull(request, CommonServiceErrorEnum.REQUEST_PARAMETER_ERROR);

        List<ApiTemplateDTO> apiTemplateDTOS = apiTemplateRepository.queryPage(request.getPage(),
            request.getRows());
        if (CollectionUtils.isEmpty(apiTemplateDTOS)) {
            return ResultBuilder.succTPage(Lists.newArrayList(), request.getPage(),
                request.getRows(), 0);
        }
        int count = apiTemplateRepository.count();

        return ResultBuilder.succTPage(apiTemplateDTOS, request.getPage(), request.getRows(),
            count);
    }

    /**
     * Query by pk t single result.
     *
     * @param request the request
     * @return the t single result
     */
    @Override
    public TSingleResult<ApiTemplateDTO> queryByPk(TRequest<Long> request) {
        ServiceAssert.notNull(request, CommonServiceErrorEnum.REQUEST_PARAMETER_ERROR);
        ServiceAssert.notNull(request.getCondition(),
            CommonServiceErrorEnum.REQUEST_PARAMETER_ERROR);
        ApiTemplateDTO dto = apiTemplateRepository.queryByPk(request.getCondition());
        return ResultBuilder.succTSingle(dto);
    }

    /**
     * 根据主键pk删除配置信息
     *
     * @param request the request
     * @return the base result
     */
    @Override
    public BaseResult deleteByPk(TRequest<Long> request) {
        ServiceAssert.notNull(request, CommonServiceErrorEnum.REQUEST_PARAMETER_ERROR);
        ServiceAssert.notNull(request.getCondition(),
            CommonServiceErrorEnum.REQUEST_PARAMETER_ERROR);
        apiTemplateMapper.deleteByPrimaryKey(request.getCondition());
        return ResultBuilder.succ();
    }
}
